package com.fick.hibernate.dao;

import java.util.Date;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

import com.fick.hibernate.model.VoteinfoIp;

/**
 * Data access object (DAO) for domain model class VoteinfoIp.
 * 
 * @see com.fick.hibernate.model.VoteinfoIp
 * @author MyEclipse Persistence Tools
 */

public class VoteinfoIpDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(VoteinfoIpDAO.class);
	// property constants
	public static final String VOTEINFO_ID = "voteinfoId";
	public static final String IP = "ip";

	public void attachClean(VoteinfoIp instance) {
		log.debug("attaching clean VoteinfoIp instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachDirty(VoteinfoIp instance) {
		log.debug("attaching dirty VoteinfoIp instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public List findAll() {
		log.debug("finding all VoteinfoIp instances");
		try {
			String queryString = "from VoteinfoIp";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public List findByExample(VoteinfoIp instance) {
		log.debug("finding VoteinfoIp instance by example");
		try {
			List results = getSession().createCriteria("com.fick.hibernate.model.VoteinfoIp").add(Example.create(instance))
					.list();
			log.debug("find by example successful, result size: " + results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public VoteinfoIp findById(java.lang.Integer id) {
		log.debug("getting VoteinfoIp instance with id: " + id);
		try {
			VoteinfoIp instance = (VoteinfoIp) getSession().get("com.fick.hibernate.model.VoteinfoIp", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByIp(Object ip) {
		return findByProperty(IP, ip);
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding VoteinfoIp instance with property: " + propertyName + ", value: " + value);
		try {
			String queryString = "from VoteinfoIp as model where model." + propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByVoteinfoId(Object voteinfoId) {
		return findByProperty(VOTEINFO_ID, voteinfoId);
	}

	// 判断一个ip今天是否投过票
	public boolean isHave(String ip, int voteinfoid) {
		boolean isHave = false;
		Date date = new Date();
		String dateStr = "" + (date.getYear() + 1900) + "-" + (date.getMonth() + 1) + "-" + date.getDate();
		String dateStrB = dateStr + " 00:00:00";
		String dateStrE = dateStr + " 23:59:59";
		int have = this.count(VoteinfoIp.class, "where voteinfo.voteinfoId=" + voteinfoid + " and ip='" + ip
				+ "' and ((addDate>'" + dateStrB + "' and addDate<'" + dateStrE + "') or addDate='" + dateStr + "')");
		if (have > 0) {
			isHave = true;
		}
		return isHave;
	}

	public VoteinfoIp merge(VoteinfoIp detachedInstance) {
		log.debug("merging VoteinfoIp instance");
		try {
			VoteinfoIp result = (VoteinfoIp) getSession().merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}
}